<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="button">按钮</div>
    <script>

        var FSM = {
            off: {
                buttonClick: function () {
                    console.log('弱光')
                    this.setState(this.weakLightState)
                }
            },
            weakLightState: {
                buttonClick: function () {
                    console.log('强光')
                    this.setState(this.strongLightState)
                }
            },
            strongLightState: {
                buttonClick: function () {
                    console.log('关灯')
                    this.setState(this.offLightState)
                }
            }
        }

        function Light() {
            this.offLightState = FSM.off
            this.weakLightState = FSM.weakLightState
            this.strongLightState = FSM.strongLightState
            this.currentState = this.offLightState
            this.button = document.getElementById('button')
            this.init()
        }

        Light.prototype.init = function () {
            this.button.onclick = () => {
                this.currentState.buttonClick.call(this)
            }
        }
        Light.prototype.setState = function (newState) {
            this.currentState = newState
        }
        new Light()
    </script>

</body>

</html>